<template>
  <el-dialog :title="`${row.id?'编辑':'添加'}优惠卷`" :visible.sync="visible" :close-on-click-modal="false" 
    :before-close="handleCloseDialog" width="700px">
    <el-form :model="dataObject" :rules="rules" ref="adminForm" label-width="130px" size="small">
    <el-form-item label="使用面额：" prop="usageAmount">
        <el-input v-model="dataObject.usageAmount"  placeholder="请输入使用面额：" :maxlength="200" clearable></el-input>
    </el-form-item>
    <el-form-item label="增值比例：" prop="addedRate">
        <el-input v-model="dataObject.addedRate"  placeholder="请输入增值比例" :maxlength="200" clearable></el-input>
    </el-form-item>
    <el-form-item label="有效期(天)：" prop="lifespan">
        <el-input v-model="dataObject.lifespan"  placeholder="请输入有效期(天)" :maxlength="200" clearable></el-input>
    </el-form-item>
     
      
    </el-form>
   
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleConfirmDialog('adminForm')" size="mini" :loading="loading">确 定</el-button>
      <el-button @click="handleCloseDialog" size="mini">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { addCouponInfo,updateCouponInfo } from '@/api/config/index';
import { createNamespacedHelpers } from "vuex";
import { getMemberLickList } from '@/api/transaction/index';
const { mapState } = createNamespacedHelpers("coupon");
const defaultObject = {
  usageAmount: "",
  addedRate: '',
  lifespan:''
};

export default {

  data() {
    return {
      visible:true,
      loading: false,
      remoteLoading:false,
      dataObject: Object.assign({}, defaultObject),
    };
  },
  props: {
    row: {
      type: Object,
      default: {}
    }
  },
   computed: {
    ...mapState({
      rules: state => state.rules
    })
  },
  mounted() {
    if (this.row.id) {
      this.getLoad();
    }
  },
  methods: {

    //初始化详情
    getLoad() {
      this.dataObject={...this.row};
    },
    handleCloseDialog() {
      this.$emit("handle-cancel");
    },
    handleConfirmDialog(formName) {
      this.$refs[formName].validate(async(valid) => {
        if (valid) {
          this.loading=true;
          let params={...this.dataObject};
          
          if(this.row.id){
            await updateCouponInfo({...params,id:this.row.id});
          }else{
            await addCouponInfo(params);
          }
          this.loading=false;
          this.resetForm(formName);
          this.$message({
            message: `${this.row.id?'新增':'编辑'}成功`,
            type: "success",
            duration: 1000
          });
          this.$emit("handle-confirm");
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dataObject = Object.assign({}, defaultObject);
    },
   

  }
};
</script>
<style scoped>

</style>